﻿/**********************************************************\
*  TinyLib - InputManager                                  *
*                                                          *
*  Handles input from keyboard, mouse and XBOX controllers.*
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace TinyLib.Input
{
    public class InputManager : GameComponent
    {
        #region Fields

        private KeyboardState currentKeyState, lastKeyState;
        private MouseState currentMouseState, lastMouseState;
        private GamePadState currentPadState, lastPadState; //gör dessa till en lista i en container istället
        private GamePadState currentPadState2, lastPadState2;
        private GamePadState currentPadState3, lastPadState3;
        private GamePadState currentPadState4, lastPadState4;

        public Vector2 mouseAbsolutePosition = Vector2.Zero;
        public Vector2 mouseRelativePosition = Vector2.Zero;

        public enum MouseButtons
        {
            Left,
            Right,
            xButton1,
            xButton2
        }

        #endregion

        #region Inits

        public InputManager(Game game)
            : base(game)
        {
        }

        #endregion

        #region Keyboard Commands

        public bool bJustPressed(Keys key)
        {
            return currentKeyState.IsKeyDown(key) && lastKeyState.IsKeyUp(key);
        }

        public bool bJustreleased(Keys key)
        {
            return currentKeyState.IsKeyUp(key) && lastKeyState.IsKeyDown(key);
        }

        public bool bIsDown(Keys key)
        {
            return currentKeyState.IsKeyDown(key);
        }

        #endregion

        #region Mouse Commands

        public bool bJustPressed(MouseButtons button)
        {
            return bIsDown(currentMouseState, button) && !bIsDown(lastMouseState, button);
        }

        public bool bJustReleased(MouseButtons button)
        {
            return !bIsDown(currentMouseState, button) && bIsDown(lastMouseState, button);
        }

        // Is button down without mousestate
        public bool bIsDown(MouseButtons button)
        {
            switch (button)
            {
                case MouseButtons.Left:
                    return currentMouseState.RightButton == ButtonState.Pressed ? true : false;
                case MouseButtons.Right:
                    return currentMouseState.LeftButton == ButtonState.Pressed ? true : false;
                case MouseButtons.xButton1:
                    return currentMouseState.XButton1 == ButtonState.Pressed ? true : false;
                case MouseButtons.xButton2:
                    return currentMouseState.XButton2 == ButtonState.Pressed ? true : false;
                default:
                    return false;
            }
        }

        // Is button down with MouseState
        public bool bIsDown(MouseState mouseState, MouseButtons button)
        {
            switch (button)
            {
                case MouseButtons.Left:
                    return currentMouseState.RightButton == ButtonState.Pressed ? true : false;
                case MouseButtons.Right:
                    return currentMouseState.LeftButton == ButtonState.Pressed ? true : false;
                case MouseButtons.xButton1:
                    return currentMouseState.XButton1 == ButtonState.Pressed ? true : false;
                case MouseButtons.xButton2:
                    return currentMouseState.XButton2 == ButtonState.Pressed ? true : false;
                default:
                    return false;
            }
        }

        #endregion

        #region GamePad Commands

        //GamePadState padState1 = GamePad.GetState(PlayerIndex.One);

        public bool bJustPressed(Buttons button, int playerNr)
        {
            switch (playerNr)
            {
                case 1:
                    return currentPadState.IsButtonDown(button) && lastPadState.IsButtonUp(button);
                case 2:
                    return currentPadState2.IsButtonDown(button) && lastPadState2.IsButtonUp(button);
                case 3:
                    return currentPadState3.IsButtonDown(button) && lastPadState3.IsButtonUp(button);
                case 4:
                    return currentPadState4.IsButtonDown(button) && lastPadState4.IsButtonUp(button);
                default:
                    return currentPadState.IsButtonDown(button) && lastPadState.IsButtonUp(button);
            }
        }

        public bool bJustReleased(Buttons button, int playerNr)
        {
            switch (playerNr)
            {
                case 1:
                    return currentPadState.IsButtonUp(button) && lastPadState.IsButtonDown(button);
                case 2:
                    return currentPadState2.IsButtonUp(button) && lastPadState2.IsButtonDown(button);
                case 3:
                    return currentPadState3.IsButtonUp(button) && lastPadState3.IsButtonDown(button);
                case 4:
                    return currentPadState4.IsButtonUp(button) && lastPadState4.IsButtonDown(button);
                default:
                    return currentPadState.IsButtonUp(button) && lastPadState.IsButtonDown(button);
            }
        }

        public bool bIsDown(Buttons button, int playerNr)
        {
            switch (playerNr)
            {
                case 1:
                    return currentPadState.IsButtonDown(button);
                case 2:
                    return currentPadState2.IsButtonDown(button);
                case 3:
                    return currentPadState3.IsButtonDown(button);
                case 4:
                    return currentPadState4.IsButtonDown(button);
                //  default:
                //    return currentPadState.IsButtonDown(button);
            }
            return false;
        }

        #endregion

        #region Update

        public override void Update(GameTime gameTime)
        {
            #region Keyboard Update

            lastKeyState = currentKeyState;
            currentKeyState = Keyboard.GetState();

            #endregion

            #region Mouse Update

            lastMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            mouseRelativePosition = new Vector2(currentMouseState.X - lastMouseState.X, currentMouseState.Y - lastMouseState.Y);
            // Mouse positions
            mouseAbsolutePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
           // mouseRelativePosition = new Vector2(lastMouseState.X - currentMouseState.X, lastMouseState.Y - currentMouseState.Y);
            

            #endregion

            #region GamePad Update

            lastPadState = currentPadState;
            currentPadState = GamePad.GetState(PlayerIndex.One);

            lastPadState2 = currentPadState2;
            currentPadState2 = GamePad.GetState(PlayerIndex.Two);

            lastPadState3 = currentPadState3;
            currentPadState3 = GamePad.GetState(PlayerIndex.Three);

            lastPadState4 = currentPadState4;
            currentPadState4 = GamePad.GetState(PlayerIndex.Four);

            #endregion

            base.Update(gameTime);
        }

        #endregion
    }
}
